$(function(){

	

    function GetQueryString (name) { //获取指定name的url参数
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return (r[2]);
        return null;
    }
    function Toast (txt){
        var toastHtml = '<div class="tk_info" id="tkInfo"></div>';
        var toast = $('body #tkInfo');
        if (toast.length) {
            toast.html(txt).show();
        }else{
            $('body').append(toastHtml);
            $('body #tkInfo').html(txt).show();
        }
        setTimeout(function(){
          $('body #tkInfo').hide();
        },2000)
    }

    const utId = GetQueryString('utId');
    const code = GetQueryString('code');
    const host =  'http://'+ GetQueryString('invokeDomain');
	//开
	$('.open').on('click',function(){
		$('.page1').hide();
		$('.page2').show();
	})
	//领取红包
	$('#lqhbBtn').on('click',function(){
		var phone = $('#phone').val();
		if (!(/^1[3456789]\d{9}$/.test(phone))) {
			Toast("手机号码有误，请重填");  
        	return false;
		}
		$.ajax({
            type:'get',
            dataType: 'json',
            url:host+'/system/api/transfer/acceptInvite',
            data: {
                'phone':phone,
                'utId':utId,
                'code':code
            },
            //请求成功时执行
            success: function(v) {
                const code = v.code;
                if (code == 0) {
                    $('.page2').hide();
                    $('.page3').show();
                    //开始倒计时
                    var oldtime = new Date(); //传入的时间
				        oldtime = oldtime.setDate(oldtime.getDate() + 1);//模拟24小时后的时间
				        //时间换算
				    function timer() {
				        var newtime = new Date();
				        var time = (oldtime - newtime) / 1000;
				        var h = parseInt(time / 60 / 60 % 24);
				        var m = parseInt(time / 60 % 60);
				        var s = parseInt(time % 60);
				        console.log(h,m,s)
				        if(h<=9) h = '0'+h;
				        if(m<=9) m = '0'+m;
				        if(s<=9) s = '0'+s;
				        $('.house').html(h)
				        $('.minute').html(m)
				        $('.second').html(s)
				        
				    }
				    setInterval(timer, 1000)
                }else{
                    Toast(v.msg);
                }
            },
            //请求失败遇到异常触发
            error: function(d) {
                Toast(d.msg);
            }
        })
	})

	function Toast (txt){
        var toastHtml = '<div class="tk_info" id="tkInfo"></div>';
        var toast = $('body #tkInfo');
        if (toast.length) {
            toast.html(txt).show();
        }else{
            $('body').append(toastHtml);
            $('body #tkInfo').html(txt).show();
        }
        setTimeout(function(){
          $('body #tkInfo').hide();
        },2000)
    }

	
})